<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once '/application/libraries/PHPExcel.php';

class Userlist extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('Users_m');
		$this->load->model('Agent_m');
		$this->load->model('Device_m');
		$this->load->model('Zipfile');
		$this->load->model('Order_m');
		$this->load->model('Recharge_m');
		ini_set('date.timezone','Asia/Shanghai');

	}
	/*会员列表*/
	public function userlists(){

		if (empty($_GET['fenye'])) {
			$total = $this->db->count_all_results('users');
			
			if (!empty($_GET['seachtext'])) {
				$seachtext = $_GET['seachtext'];
				$data=$this->db->like('nickname', $seachtext, 'both')
				->from('users a')
				->join('users_weixin b','b.user_id=a.user_id')
				->order_by('a.user_id desc')
				->limit('10')
				->get()
				->result_array();
				// echo $this->db->last_query();exit;
				$res['datas'] = $data;
				$res['count'] = $total;
				$res['limit'] = 10;
				echo _echo($res);exit;

			}else{
				$data=$this->db->select('a.user_id,a.nickname,a.reg_time,b.openid')
				->from('users a')
				->join('users_weixin b','b.user_id=a.user_id')
				->order_by('a.user_id desc')
				->limit('10')
				->get()
				->result_array();

				$res['datas'] = $data; 
				$res['count'] = $total;
				$res['limit'] = 10;
				echo _echo($res);exit;
				
			}
		}else{
			$cur_fenye = $_GET['fenye'];//当前页
			$limit = ($cur_fenye - 1) * 10;//当前页数据起始值
			$offset = 10;//显示的数据
			if (!empty($_GET['seachtext'])) {
				$seachtext = $_GET['seachtext'];
				$data=$this->db->like('nickname', $seachtext, 'both')
				->from('users a')
				->join('users_weixin b','b.user_id=a.user_id')
				->order_by('a.user_id desc')
				->limit($offset,$limit)
				->get()
				->result_array();
				// echo $this->db->last_query();exit;
				$res['datas'] = $data;
				echo _echo($res);exit;

			}else{

				$data=$this->db->select('a.user_id,a.nickname,a.reg_time,b.openid')
				->from('users a')
				->join('users_weixin b','b.user_id=a.user_id')
				->order_by('a.user_id desc')
				->limit($offset,$limit)
				->get()
				->result_array();
				$res['datas'] = $data;
				echo _echo($res);exit;
			}
		}
		


	}
	/*设备列表*/
	public function devicelists(){
		$order_by = 'id desc';
		$where['leader'] = 0;
		$daili = $this->Agent_m->select($where,$order_by);
		if (empty($_GET['fenye'])) {

			$total = $this->db->count_all_results('device');
			if (!empty($_GET['seachtext'])) {
				$seachtext = $_GET['seachtext'];
				$data=$this->db->like('device_sn', $seachtext, 'both')
				->from('device')
				->order_by('device_id desc')
				->limit('10')
				->get()
				->result_array();
			}else{

				$data = $this->Device_m->select('','device_id desc',10);
				
			}
			foreach ($data as $key => $val) {
				$mm1['id'] = $val['agent1'];
				$dan1 = $this->Agent_m->get_one($mm1);
				$mm2['id'] = $val['agent2'];
				$dan2 = $this->Agent_m->get_one($mm2);

				if (!empty($dan1)) {
					$data[$key]['agent1'] = $dan1['agent_name'];
				}else{
					$data[$key]['agent1'] = '无';
				}

				if (!empty($dan2)) {
					$data[$key]['agent2'] = $dan2['agent_name'];
				}else{
					$data[$key]['agent2'] = '无';
				}
			}
			$res['daili'] = $daili;//代理
			$res['datas'] = $data;//设备
			$res['count'] = $total;
			$res['limit'] = 10;
			echo _echo($res);exit;
		}else{
			$cur_fenye = $_GET['fenye'];//当前页
			$limit = ($cur_fenye - 1) * 10;//当前页数据起始值
			$offset = 10;//显示的数据

			if (!empty($_GET['seachtext'])) {
				$seachtext = $_GET['seachtext'];
				$data=$this->db->like('device_sn', $seachtext, 'both')
				->from('device')
				->order_by('device_id desc')
				->limit($offset,$limit)
				->get()
				->result_array();
			}else{
				$data = $this->Device_m->select('','device_id desc',$offset,$limit);
			}
			
			foreach ($data as $key => $val) {
				$mm1['id'] = $val['agent1'];
				$dan1 = $this->Agent_m->get_one($mm1);
				$mm2['id'] = $val['agent2'];
				$dan2 = $this->Agent_m->get_one($mm2);

				if (!empty($dan1)) {
					$data[$key]['agent1'] = $dan1['agent_name'];
				}else{
					$data[$key]['agent1'] = '无';
				}

				if (!empty($dan2)) {
					$data[$key]['agent2'] = $dan2['agent_name'];
				}else{
					$data[$key]['agent2'] = '无';
				}
			}
			$res['daili'] = $daili;//代理
			$res['datas'] = $data;//设备
			echo _echo($res);exit;

			
		}

	}
	/*查询一级代理下的代理*/
	public function update_agents(){

		if ($_POST['agentone'] == 0) {
			echo _echo(1);exit;//选择的不是代理
		}else{
			$where['agent1'] = $_POST['agentone'];
			$deviceArr = $_POST['deviceArr'];
			foreach ($deviceArr as $key => $val) {
				$where['device_id'] = $val;
				$device_data[] = $this->Device_m->select($where);
			}
			//数组去重过后若只有一个表示只有一个一级代理，2个以上就是有多个一级代理
			$newArr = array();
			foreach($device_data as $key=>$val){
				//三维降二维
			    foreach($val as $k=>$v){
			      $newArr[] = $v['agent1'];
			    }
			}
			$count_arr = count(array_unique($newArr));
			if ($count_arr > 1) {
				echo _echo(2);exit;//表示有多个一级代理
			}else{
				$wheres = array('leader'=>$_POST['agentone']);
				$two_agent = $this->Agent_m->select($wheres);
				echo _echo($two_agent);exit;
			}
		}
	}
	/*修改哪些设备属于二级代理*/
	public function update_agenttwos(){
		$update1['agent1'] = $_POST['agentone'];
		$update2['agent2'] = $_POST['agenttwo'];
		$deviceArrs = $_POST['deviceArrs'];
		
		if ($update2['agent2'] == 0) {
			//只修改一级
			foreach ($deviceArrs as $key => $val) {
				$where['device_id'] = $val;
				$this->Device_m->update($where,$update1);
			}
		}else{
			//2级都要修改
			foreach ($deviceArrs as $key => $val) {
				$where['device_id'] = $val;
				$this->Device_m->update($where,$update1);
				$this->Device_m->update($where,$update2);
			}
		}
		echo _echo(1);exit;
	}

	/*点击下载二维码*/
	public function down_erweimas(){
		$this->load->helper('uploads');
		$data = file_get_contents(APPPATH."uploads/QRcodeDevice/");
		$name = 'down';
		force_download($name, $data);

	// 	$path = './uploads/QRcodeDevice/';//文件路径
	// 	$aa = $this->Zipfile->read_file($path);//允许你压缩一个服务器某处存在的文件 ，一个文件路径
	// 	$url = './uploads/QRcodeDevice/'.time().'.zip';
	// 	$this->Zipfile->archive($url);//将Zip压缩文件写入服务器的一个目录下
	// 	print_r($aa);exit;

	// 	// $dfile = tempnam('./uploads/QRcodeDevice/', 'tmp');//产生一个临时文件，用于缓存下载文件
 //  //       $filename = 'erweima'.'.zip'; //下载的默认文件名
 //  //       // $image  = M('Device')->field('erweima')->select();
 //  //       // foreach($image as $v){
 //  //       //     $image_path = './public/upload/qr_code/';
 //  //       //      $zip->add_path($image_path);
 //  //       //      // $zip->add_file(file_get_contents($v['erweima']), $v['erweima']);
 //  //       //      // 添加打包的图片，第一个参数是图片内容，第二个参数是压缩包里面的显示的名称, 可包含路径
 //  //       //      // 或是想打包整个目录 用 
 //  //       // }
 //  //       $image_path = './uploads/QRcodeDevice/';
 //  //       // $zip->load->add_path($image_path);
 //  //       // $zip->load->output($dfile);
 //  //       $aa = $this->Zipfile->add_path($image_path);
 //  //       print_r($aa);exit;
 //  //       $this->Zipfile->output($dfile);
 //  //       // 下载文件
 //  //       ob_clean();
 //  //       header('Pragma: public');
 //  //       header('Last-Modified:'.gmdate('D, d M Y H:i:s') . 'GMT');
 //  //       header('Cache-Control:no-store, no-cache, must-revalidate');
 //  //       header('Cache-Control:pre-check=0, post-check=0, max-age=0');
 //  //       header('Content-Transfer-Encoding:binary');
 //  //       header('Content-Encoding:none');
 //  //       header('Content-type:multipart/form-data');
 //  //       header('Content-Disposition:attachment; filename="'.$filename.'"'); //设置下载的默认文件名
 //  //       header('Content-length:'. filesize($dfile));
 //  //       $fp = fopen($dfile, 'r');
 //  //       while(connection_status() == 0 && $buf = @fread($fp, 8192)){
 //  //        echo $buf;
 //  //       }
 //  //       fclose($fp);
 //  //       @unlink($dfile);
 //  //       @flush();
 //  //       @ob_flush();
 //  //       exit();
	}

	/*订单列表*/
	public function orderList(){
		if (empty($_GET['fenye'])) {
			$limit = 10;
			$order_by = 'orderid desc';
			$where = '';
			$orderlist = $this->Order_m->select($where,$order_by,$limit);

			$num = "select count(*) as count from mifi_order ";
			$c = $this->db->query($num);
			$one = $c->result_array(); 
			foreach ($orderlist as $key => $val) {
				//查询套餐
				if ($val['goods_on']) {
					$taos = $this->Recharge_m->get_one(array('package_id'=>$val['goods_on']));
					$orderlist[$key]['taocan'] = $taos['flow'];
				}else{
					$orderlist[$key]['taocan'] = '';
				}
				//查询用户
				if ($val['user_id'] == 0) {
					$orderlist[$key]['username'] = '';
				}else{
					$user = $this->Users_m->get_one(array('user_id'=>$val['user_id']));
					$orderlist[$key]['username'] = $user['nickname'];
				}
				//查询代理商
				$device = $this->Device_m->get_ones(array('device_sn'=>$val['device_id']));
				$oneDaili = $this->Agent_m->get_one(array('id'=>$device['agent1']));
				$twoDaili = $this->Agent_m->get_one(array('id'=>$device['agent2']));
				if ($oneDaili['agent_name']) {
					$orderlist[$key]['oneDaili'] = $oneDaili['agent_name'];
				}else{
					$orderlist[$key]['oneDaili'] = '';
				}
				if ($twoDaili['agent_name']) {
					$orderlist[$key]['twoDaili'] = $twoDaili['agent_name'];
				}else{
					$orderlist[$key]['twoDaili'] = '';
				}
			}
			$res['datas'] = $orderlist;
			$res['count'] = $one[0]['count'];
			$res['limit'] = 10;
			echo _echo($res);exit;

		}else{

			$cur_fenye = $_GET['fenye'];//当前页
			$limit = ($cur_fenye - 1) * 10;//当前页数据起始值
			$offset = 10;//显示的数据
			$order_by = 'orderid desc';
			$where = '';

			$num = "select count(*) as count from mifi_order ";
			$c = $this->db->query($num);
			$one = $c->result_array();

			$sql = "select * from mifi_order order by orderid desc limit ".$limit.",".$offset."";
			$ab = $this->db->query($sql);
			$orderlist = $ab->result_array(); 
			foreach ($orderlist as $key => $val) {
				//查询套餐
				if ($val['goods_on']) {
					$taos = $this->Recharge_m->get_one(array('package_id'=>$val['goods_on']));
					$orderlist[$key]['taocan'] = $taos['flow'];
				}else{
					$orderlist[$key]['taocan'] = '';
				}
				//查询用户
				if ($val['user_id'] == 0) {
					$orderlist[$key]['username'] = '';
				}else{
					$user = $this->Users_m->get_one(array('user_id'=>$val['user_id']));
					$orderlist[$key]['username'] = $user['nickname'];
				}
				//查询代理商
				$device = $this->Device_m->get_ones(array('device_sn'=>$val['device_id']));
				$oneDaili = $this->Agent_m->get_one(array('id'=>$device['agent1']));
				$twoDaili = $this->Agent_m->get_one(array('id'=>$device['agent2']));
				if ($oneDaili['agent_name']) {
					$orderlist[$key]['oneDaili'] = $oneDaili['agent_name'];
				}else{
					$orderlist[$key]['oneDaili'] = '';
				}
				if ($twoDaili['agent_name']) {
					$orderlist[$key]['twoDaili'] = $twoDaili['agent_name'];
				}else{
					$orderlist[$key]['twoDaili'] = '';
				}
			}
			$res['datas'] = $orderlist;
			echo _echo($res);exit;
		}

	}

	/*代理列表*/
	public function onedailiList(){
		if (empty($_GET['fenye'])) {
			$num = "select count(*) as count from mifi_agent ";
			$c = $this->db->query($num);
			$one = $c->result_array(); 
			$sql = "select * from mifi_agent where agent_level=1 order by id desc limit 0,10";
			$ab = $this->db->query($sql);
			$res['datas'] = $ab->result_array(); 
			$res['count'] = $one[0]['count'];
			$res['limit'] = 10; 
			echo _echo($res);exit;
		}else{
			$cur_fenye = $_GET['fenye'];//当前页
			$limit = ($cur_fenye - 1) * 10;//当前页数据起始值
			$offset = 10;//显示的数据
			
			$num = "select count(*) as count from mifi_agent ";
			$c = $this->db->query($num);
			$one = $c->result_array();
			$sql = "select * from mifi_agent where agent_level=1 order by id desc limit ".$limit.",".$offset."";
			$ab = $this->db->query($sql);
			$res['datas'] = $ab->result_array(); 
			echo _echo($res);exit;

			
		}
	}
	public function twodailiList(){
		if (empty($_GET['fenye'])) {
			$num = "select count(*) as count from mifi_agent ";
			$c = $this->db->query($num);
			$one = $c->result_array(); 
			$sql = "select * from mifi_agent where agent_level=2 order by id desc limit 0,10";
			$ab = $this->db->query($sql);
			$res['datas'] = $ab->result_array(); 
			$res['count'] = $one[0]['count'];
			$res['limit'] = 10; 
			echo _echo($res);exit;
		}else{
			$cur_fenye = $_GET['fenye'];//当前页
			$limit = ($cur_fenye - 1) * 10;//当前页数据起始值
			$offset = 10;//显示的数据
			
			$num = "select count(*) as count from mifi_agent ";
			$c = $this->db->query($num);
			$one = $c->result_array();
			$sql = "select * from mifi_agent where agent_level=2 order by id desc limit ".$limit.",".$offset."";
			$ab = $this->db->query($sql);
			$res['datas'] = $ab->result_array(); 
			echo _echo($res);exit;
		}
	}
	/*代理详情*/
	public function daili_details(){
		if (empty($_GET['fenye'])) {
			$daili_id = $_GET['daili_id'];

			$dai = array('id'=>$daili_id);
			$level = $this->Agent_m->get_one($dai);
			if ($level['agent_level'] == 1) {
				$num = "select count(*) as count from mifi_device where agent1=".$daili_id."";
				$cs = $this->db->query($num);
				$bbs = $cs->result_array(); 
				$det['agent1'] = $daili_id;
			}else{
				$num = "select count(*) as count from mifi_device where agent2=".$daili_id."";
				$cs = $this->db->query($num);
				$bbs = $cs->result_array(); 
				$det['agent2'] = $daili_id;
			}

			$order_bys = 'device_id desc';
			$arr = $this->Device_m->select($det,$order_bys,10);
			
			$res['datas'] = $arr;//设备
			$res['count'] = $bbs[0]['count'];
			$res['limit'] = 10;
			echo _echo($res);exit;

		}else{
			$cur_fenye = $_GET['fenye'];//当前页
			$limit = ($cur_fenye - 1) * 10;//当前页数据起始值
			$offset = 10;//显示的数据
			
			$daili_id = $_GET['daili_id'];
			$dai = array('id'=>$daili_id);
			$level = $this->Agent_m->get_one($dai);
			
			if ($level['agent_level'] == 1) {
				$sql = "select * from mifi_device where agent1=".$daili_id." order by device_id desc limit ".$limit.",".$offset."";
				$ab = $this->db->query($sql);
				$res['datas'] = $ab->result_array(); 
			}else{
				$sql = "select * from mifi_device where agent2=".$daili_id." order by device_id desc limit ".$limit.",".$offset."";
				$ab = $this->db->query($sql);
				$res['datas'] = $ab->result_array(); 
			}
			echo _echo($res);exit;

		}

	}
}
